package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {
    // 利用RabbitListener来声明监听的队列信息，将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(queues = "simple.queue")
    public void listenQueueMessage(String msg) {
        System.out.println("spring 消费者接收到的消息：+【"+msg+"】");
    }

    // 一个队列，绑定多个消费者
    @RabbitListener(queues = "work.queue")
    public void listenQueueMessage1(String msg) {
        System.out.println("spring 消费者1接收到的消息：+【"+msg+"】");
    }
    @RabbitListener(queues = "work.queue")
    public void listenQueueMessage2(String msg) {
        System.err.println("spring 消费者2接收到的消息.......：+【"+msg+"】");
    }

    // fanout 交换机
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutMessage1(String msg) {
        System.out.println("spring 消费者1接收到fanout.queue1的消息：+【"+msg+"】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutMessage2(String msg) {
        System.out.println("spring 消费者2接收到fanout.queue2的消息：+【"+msg+"】");
    }

    // direct 交换机
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectMessage1(String msg) {
        System.out.println("spring 消费者1接收到direct.queue1的消息：+【"+msg+"】");
    }
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectMessage2(String msg) {
        System.out.println("spring 消费者2接收到direct.queue2的消息：+【"+msg+"】");
    }

    // topic 交换机
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.out.println("spring 消费者1接收到topic.queue1的消息：+【"+msg+"】");
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.out.println("spring 消费者2接收到topic.queue2的消息：+【"+msg+"】");
    }

    // 消息转换器json
    @RabbitListener(queues = "object.queue")
    public void listenMessageQueue2(Map<String,Object> msg) {
        System.out.println("spring 消费者接收到object.queue的消息：+【"+msg+"】");
    }

}
